<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/21
 * Time: 11:34
 * 菜单模型
 */
namespace app\admin\model;
use think\Model;
use app\admin\model\RoleMenus;

class Menus extends Model
{
    /**
     * @todo 根据父级id获取所属子级菜单
     */
    public function getChildMenu($pid = 0)
    {
        return $this->where(['pid' => $pid, 'status' => 1])->order('order_no DESC')->column('name', 'id');
    }

    /**
     * @todo 根据pids串获取对应菜单名
     * $pids 菜单id集合字符串，以英文逗号拼接
     * $conn 菜单名之间连接符
    */
    public function getPids($pids = 0, $conn='->')
    {
        $pids_data[0] = '顶级菜单';
        if($pids === '0'){
            return $pids_data[0];
        }
        //$where['status']  = 1;
        //$where['id'] = ['in', $pids];
        //$list = $this->where($where)->column('name', 'id');
        $list = $this->whereIn('id', $pids)->column('name', 'id');
        $all = $pids_data + $list;
        $pids_arr = explode(',', $pids);
        $rs = '';
        foreach($pids_arr as $v){
            $rs .= $all[$v] . $conn;
        }
        $rs = trim($rs, $conn);
        return $rs;
    }

    /**
     * @todo 验证菜单是否存在
     * @param type $menus
     * @return boolean
     */
    public function validateMenus($menus)
    {
        if (empty($menus) || !is_array($menus)) {
            return false;
        }
        $ret = $this->where('id', 'IN', $menus)->select();
        if ($ret) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @todo 根据菜单链接获取
     * @param string $menu_url
     * @return int id
    */
    public function getIdByMenuUrl($menu_url)
    {
        if(empty($menu_url)){
            return false;
        }
        $menu_url = trim($menu_url , '\/');
        $where['status'] = 1;
        $where['menu_url'] = $menu_url;
        $id = $this->where($where)->value('id');
        return $id;
    }

    /**
     * @todo 根据角色IDS获取菜单权限
     * @param type $role_ids
     * @return boolean|array();
     */
    public function getMenusByRoles($role_ids){
        if(empty($role_ids)){
            return false;
        }
        //根据角色获取菜单IDS
        $model = new RoleMenus();
        $menu_ids = $model->getMenusByRoleIds($role_ids);
        if(empty($menu_ids)){
            return false;
        }
        return $this->getMenusByMenuIds($menu_ids);
    }

    /**
     * @todo 根据菜单ids获取菜单
     * @param $menu_ids
     * @return array
    */
    public function getMenusByMenuIds($menu_ids)
    {
        if(empty($menu_ids)){
            return false;
        }
        $list = $this->where('status', '1')->where('id', 'in', $menu_ids)->order('order_no DESC, id ASC')->select();
        if(empty($list)){
            return false;
        }
        $menus = array();
        foreach($list as $item){
            $menus[$item['id']] = $item;
        }
        return $menus;

    }

    /**
     * @todo 获取管理员权限菜单
     * @abstract 排序：一级菜单，排序，二级菜单
     * @return boolean
     */
    public function getAdminMenus(){
        $list = $this->where('status', 1)->order('order_no DESC, id ASC')->select();
        if(empty($list)){
            return false;
        }
        $menus = array();
        foreach($list as $item){
            $menus[$item['id']] = $item;
        }
        return $menus;
    }

}